#include <netinet/in.h>
#include <sys/types.h>    // for socket
#include <sys/socket.h>   // for socket
#include <stdio.h>       // for printf
#include <stdlib.h>       // for exit
#include <string.h>       // for bzero
#include <pthread.h>

//#define SERVER_IP				"192.168.137.1"
#define SERVER_IP				"192.168.1.100"
//#define SERVER_IP 			"172.17.41.230"
#define SERVER_PORT				5208
#define LENGTH_OF_LISTEN_QUEUE  20
#define BUFFERSIZE 				1023
#define FILENAMESIZE			(255+1)

#define PRINT_FUNC() \
	do{\
		printf("FILE:%s LINE:%d FUNC:%s\n", __FILE__, __LINE__, __func__);\
	}while(0)

#define GETFILESIZE(fp, size) \
	do{\
		int tsize = ftell(fp);\
		fseek(fp, 0, SEEK_END);\
		size = ftell(fp);\
		fseek(fp, tsize, SEEK_SET);\
	}while(0)

int creatConn()
{
	int ret = 0;
	struct sockaddr_in sSockAddr;
	bzero(&sSockAddr, sizeof(struct sockaddr_in));
	sSockAddr.sin_family = AF_INET;
	sSockAddr.sin_port   = htons(SERVER_PORT);
	if((inet_aton(SERVER_IP, &sSockAddr.sin_addr)) == 0)
	{
		perror("IP error!\n");
		ret = -1;
	}
	
	int client = socket(AF_INET, SOCK_STREAM, 0);
	if(client < 0)
	{
		perror("create socket error!\n");
		ret = -2;
	}
	
	if(connect(client, (struct sockaddr*)&sSockAddr, \
				sizeof(struct sockaddr)) < 0)
	{
		perror("connect to server error!\n");
		ret = -3;
	}
	
	return client;
}

int sendFile(int socket, const char* desfileName, char* srcfileName, int jobID)
{
	struct filePro{
		long int filesize;
		char fileName[256];
	}file;
	
	//打开文件，顺便判断文件是否存在
	FILE *fp = fopen(srcfileName, "r");
	if(fp == NULL)
	{
		fprintf(stderr, "FILE:%s NOT FOUND!\n", srcfileName);
		return -1;
	}
	
	//获取文件大小
	long int size;
	GETFILESIZE(fp, size);
	printf("fileName:%s  size:%ld\n", srcfileName, size);
	
	//为消息（file）赋值
	bzero(&file, sizeof(struct filePro));
	strncpy(file.fileName, desfileName, strlen(desfileName));
	file.filesize = size; 
	
	//发送文件属性到服务器端（Web服务器），判断是否可以传输文件
	int ret = send(socket, (char*)&file, sizeof(struct filePro), 0);
	if(ret <= 0)
	{
		fprintf(stderr, "send file property error!func: %s\n", __func__);
		return -2;
	}
	//等待结果反馈
	//如果不能发送文件，返回-3
	char command[6];
	bzero(command, 6);
	ret = recv(socket, command, sizeof(command), 0);
	if(strcmp(command, "begin") == 0)
	{
		printf("start to send file!\n");
	}
	else
	{
		printf("cannot send file!\n");
		return -3;
	}
	
	//发送文件正文
	int sum  = 0;
	char buff[BUFFERSIZE+1];
	bzero(buff, BUFFERSIZE+1);
	
	struct value{
		int jobID;
		float errorValue;
	}Value;
	
	float errorValue;
	int len = 0;
	//len = fread(&errorValue, sizeof(float), 1, fp);
	len = fscanf(fp, "%f", &errorValue);
	fgetc(fp);
	if(len > 0)
	{
		printf("error = %f\n", errorValue);
		Value.jobID = jobID;
		Value.errorValue = errorValue;
		send(socket, &Value, sizeof(struct value), 0);
	}
	else
	{
		return -4;
	}
	
	while((len = fread(buff, sizeof(char), BUFFERSIZE, fp)) > 0)
	{
		printf("len = %d \n", len);
		sum += len;
		if(send(socket, buff, len, BUFFERSIZE) < 0)
		{
			fprintf(stderr, "send file failed!\n");
			fclose(fp);
			return -5;
		}
		bzero(buff, BUFFERSIZE+1);
	}
	fclose(fp);
	printf("send file success!\n");
	return 0;
}

